<template>
  <el-dialog
    :title="!dataForm.id ? '消息推送新增' : '消息推送修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-input type="hidden" v-model="dataForm.msgType"></el-input>
      <el-form-item label="推送范围" prop="msgScope">
        <el-select v-model="dataForm.msgScope" placeholder="请选择推送范围">
          <el-option v-for="item in options" :key="item.val" :label="item.label" :value="item.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消息名称" prop="msgName">
        <el-input v-model="dataForm.msgName" placeholder="请输入消息名称"></el-input>
      </el-form-item>
      <el-form-item label="消息内容" prop="msgContext">
        <el-input type="textarea" v-model="dataForm.msgContext" placeholder="请输入消息内容"></el-input>
      </el-form-item>
      <el-form-item label="跳转链接" prop="jumpLink">
        <el-input v-model="dataForm.jumpLink" placeholder="请输入跳转链接"></el-input>
      </el-form-item>
      <el-form-item label="推送频次" prop="pushRate">
        <el-radio v-model="dataForm.pushRate" label="0" border @change="radioBtn" :disabled="dataForm.id>0">单次
        </el-radio>
        <el-radio v-model="dataForm.pushRate" label="1" border @change="radioBtn" :disabled="dataForm.id>0">每天
        </el-radio>
      </el-form-item>
      <el-form-item label="推送时间" prop="immediatelyState">
        <el-radio v-model="dataForm.immediatelyState" label="1" border :disabled="dataForm.id>0">立即推送</el-radio>
        <el-radio v-model="dataForm.immediatelyState" label="0" border :disabled="dataForm.id>0">定时推送</el-radio>
        <template>
          <div style="display: inline-block;" v-if="dataForm.immediatelyState === '0'">
            <div style="display: inline-block;">
              <el-time-picker
                v-if="dataForm.pushRate == 1"
                v-model="dataForm.pushTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择推送时间"
                :disabled="dataForm.id>0"
              ></el-time-picker>
              <el-date-picker
                v-else
                v-model="dataForm.pushTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择推送时间"
                :disabled="dataForm.id>0">
              </el-date-picker>
            </div>
          </div>
        </template>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible=false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      const validatePushTimeType = (rule, value, callback) => {
        if (!this.dataForm.pushTime) {
          console.log('请选择推送时间')
          callback(new Error('请选择推送时间'))
        } else {
          callback()
        }
      };
      return {
        options: [
          {
            val: "0",
            label: "全部用户"
          },
          {
            val: "1",
            label: "非会员"
          },
          {
            val: "2",
            label: "会员"
          }
        ],
        visible: false,
        dataForm: {
          id: "",
          msgName: "",
          msgScope: "",
          msgType: "0",
          pushRate: "",
          pushTime: "",
          pushState: "",
          jumpLink: "",
          immediatelyState: "",
          isAlive: "",
          msgContext: ""
        },

        dataRule: {
          msgName: [
            {required: true, message: "消息名称不能为空", trigger: "blur"}
          ],
          msgContext: [
            {required: true, message: "消息内容不能为空", trigger: "blur"}
          ],
          jumpLink: [
            {pattern: /(http|https|yiban):\/\/([\w.]+\/?)\S*/, message: '只能输入以http、https、yiban开头的网址', trigger: 'blur'}
          ],
          msgScope: [
            {required: true, message: "推送范围不能为空", trigger: "change"}
          ],
          immediatelyState: [
            {required: true, message: "推送时间不能为空", trigger: "blur"}
          ],
          pushRate: [
            {required: true, message: "推送频次不能为空", trigger: "blur"}
          ]
        },
        disabled: false
      };
    },
    mounted() {
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields();
          // this.changetime = false;
          this.disabled = false;
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(
                `/user/msgtemplate/info/${this.dataForm.id}`
              ),
              method: "get",
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.msgTemplate.id;
                this.dataForm.msgName = data.msgTemplate.msgName;
                this.dataForm.msgScope = data.msgTemplate.msgScope + "";
                this.dataForm.pushRate = data.msgTemplate.pushRate + "";
                this.dataForm.pushTime = data.msgTemplate.pushTime;
                this.dataForm.jumpLink = data.msgTemplate.jumpLink;
                this.dataForm.immediatelyState =
                  data.msgTemplate.immediatelyState + "";
                this.dataForm.msgContext = data.msgTemplate.msgContext;
                this.radioBtn();
                this.timePush();
              }
            });
          } else {
            // 修改之后再新增日期会变成修改的那天
            this.dataForm.pushTime = ''
          }
        });
      },
      //修改推送频次
      radioBtn() {
        if (this.dataForm.pushRate == "1") {
          this.disabled = true;
        } else {
          this.disabled = false;
        }
      },
      //修改推送方式
      timePush() {
        if (this.dataForm.immediatelyState == "0") {
          this.changetime = true;
        } else {
          this.changetime = false;
        }
      },
      // 表单提交
      dataFormSubmit() {
        var obj = {};
        obj.id = this.dataForm.id || undefined;
        obj.msgName = this.dataForm.msgName;
        obj.msgScope = this.dataForm.msgScope;
        obj.msgType = this.dataForm.msgType;
        obj.pushRate = this.dataForm.pushRate + '';
        if (this.dataForm.immediatelyState == "0") {
          obj.pushTime = this.dataForm.pushTime;
        }
        console.log(this.dataForm.pushTime);
        obj.jumpLink = this.dataForm.jumpLink;
        obj.immediatelyState = this.dataForm.immediatelyState;
        obj.msgContext = this.dataForm.msgContext;
        console.log(obj, 777)
        this.$refs["dataForm"].validate(valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(
                `/user/msgtemplate/${!this.dataForm.id ? "save" : "update"}`
              ),
              method: "post",
              data: this.$http.adornData(obj)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList", this.msgType);
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        });
      }
    }
  };
</script>
